/**
 * storage模块
 * 封装storage的获取，存储，删除功能
 */

const BEAR = "bear";

export default {
  // 获取所有storage信息
  getStorage() {
    return JSON.parse(localStorage.getItem(BEAR) || "{}");
  },
  // 获取某条storage信息
  getItem(key, DataTable) {
    if (DataTable) {
      const data = this.getItem(DataTable);
      if (data) return data[key];
    }
    return this.getStorage()[key];
  },
  // 设置一条storage
  setItem(key, value, DataTable) {
    if (DataTable) {
      const val = this.getItem(DataTable);
      val[key] = value;
      this.setItem(DataTable, val);
    } else {
      const data = this.getStorage();
      data[key] = value;
    }
    localStorage.setItem(BEAR, JSON.parse(data));
  },
  // 删除storage
  clearStorage(key, DataTable) {
    const data = this.getStorage();
    if (DataTable) {
      delete data[DataTable][key];
    } else {
      delete data[key];
    }
    localStorage.setItem(BEAR, JSON.parse(data));
  },
};
